<?php

namespace IDock\Insurance\ZFIC\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\ZFIC\ZFICEnums;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllZFICResponse
{

    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();

        if (!$this->dataSourceCheck()) {
            $this->printer = $renewalInquiryPrinter;
            return $this;
        }
        $body = $this->getBody();

        if ($body) {
            $renewalInquiryPrinter->setRenewalFlg(true);
        }

        $applicantPrinter = new PremiumRelationPrinter();
        $insuredPrinter = new PremiumRelationPrinter();
        $ownerPrinter = new PremiumRelationPrinter();
        $vehiclePrinter = new VehiclePrinter();

        foreach ($body as $item) {
            //投保人
            $this->setRelation($applicantPrinter, $item['RelatedParty'][0] ?? []);
            //车主
            $this->setOwner($ownerPrinter, $item['ItemMotorList'][0]['ItemMotor'][0] ?? []);
            //被保人
            $this->setInsuredRelation($insuredPrinter, $item['RiskRelatedParty'][0] ?? []);
            //车辆信息
            $this->setVehicle($renewalInquiryPrinter, $vehiclePrinter, $item['ItemMotorList'][0]['ItemMotor'][0] ?? []);
            //险种
            $this->setCoverage($renewalInquiryPrinter, $item['KindList'][0]['Kind'] ?? []);
        }
        $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
        $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
        $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
        $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }

    /**
     * 数据源检查
     * @return bool
     * @version 4.0
     */
    private function dataSourceCheck(): bool
    {
        $body = $this->get('data', []);
        if (!$body) {
            return false;
        }

        if ($body == 'Nothing') {
            return false;
        }

        $body = json_decode($body, true)[0]['PolicyListData'] ?? [];
        if (!$body) {
            return false;
        }
        $this->setBody($body);

        return true;
    }

    /**
     * 车主
     * @param PremiumRelationPrinter $printer
     * @param $data
     * @return void
     */
    private function setOwner(PremiumRelationPrinter $printer, $data)
    {
        $printer->name = $data['CarOwner'] ?? '';
        $printer->certificateNo = $data['VehicleOwnerIDNumber'] ?? '';

        if ($data['CertificateDate'] ?? '') {
            $printer->certificateStartDate = date('Y-m-d H:i:s',strtotime($data['CertificateDate']));
        }

        if ($data['VehicleTypeOfOwner']?? '') {
            if ($data['VehicleTypeOfOwner'] == '01') {
                $printer->typeCode = '01';
                $printer->certificateTypeCode = '01';
            } else {
                $printer->typeCode = '03';
                $printer->certificateTypeCode = '50';
            }
        }
    }

    /**
     * 被保人
     * @param PremiumRelationPrinter $printer
     * @param $data
     * @return void
     */
    private function setInsuredRelation(PremiumRelationPrinter $printer, $data)
    {
        $printer->name = $data['InsuredName'] ?? '';
        $printer->mobile = $data['InsuredMobilePhone'] ?? '';
        $printer->address = $data['InsuredAddress'] ?? '';
        $printer->certificateNo = $data['IdentifyNumber'] ?? '';
        $printer->certificateTypeCode = array_keys(ZFICEnums::$certificateTypeCode, $data['IdentifyType'] ?? '')[0] ?? '';
        $printer->email = $data['Email'] ?? '';
        $printer->typeCode = ($data['InsuredType'] ?? '1') == '1' ? "01" : "03";
    }

    /**
     * 投保人
     * @param PremiumRelationPrinter $printer
     * @param $data
     * @return void
     * @version 4.0
     */
    private function setRelation(PremiumRelationPrinter $printer, $data)
    {
        $printer->name = $data['AppliName'] ?? '';
        $printer->mobile = $data['MobilePhone'] ?? '';
        $printer->address = $data['InsuredAddress'] ?? '';
        $printer->certificateNo = $data['IdentifyNumber'] ?? '';
        $printer->certificateTypeCode = array_keys(ZFICEnums::$certificateTypeCode, $data['IdentifyType'] ?? '')[0] ?? '';
        $printer->email = $data['Email'] ?? '';
        $printer->typeCode = ($data['InsuredType'] ?? '1') == '1' ? "01" : "03";
    }

    /**
     * 车辆信息
     * @param RenewalInquiryPrinter $inquiryPrinter
     * @param VehiclePrinter $vehiclePrinter
     * @param $data
     * @return void
     * @version 4.0
     */
    private function setVehicle(RenewalInquiryPrinter $inquiryPrinter, VehiclePrinter $vehiclePrinter, $data)
    {
        $inquiryPrinter->firstRegisterDate = date('Y-m-d', strtotime($data["SingeinDate"] ?? ''));
        $inquiryPrinter->engineNo = $data['EngineNo'] ?? '';
        $inquiryPrinter->vin = $data['FrameNo'] ?? '';
        $inquiryPrinter->licenseNo = $data['LicenseNo'] ?? '';
        //车辆信息
        $vehiclePrinter->vehicleName = $data['NoticeType'] ?? '';
        $vehiclePrinter->brandName = $data['BrandName'] ?? '';
        $vehiclePrinter->familyName = $data['FamilyName'] ?? '';
        $vehiclePrinter->power = $data['Power'] ?? '';
        $vehiclePrinter->vehiclePrice = $data['PurchasePrice'] ?? '';
        $vehiclePrinter->vehiclePriceTax = $data['PurchasePrice'] ?? '';
        $vehiclePrinter->vehicleExhaust = $data['ExhaustScale'] ?? '';
        $vehiclePrinter->vehicleSeat = $data['SeatCount'] ?? '';
        $vehiclePrinter->vehicleYear = '';
        $vehiclePrinter->vehicleTonnage = $data['TonCount'] ?? '';
        $vehiclePrinter->wholeWeight = '';
    }

    /**
     * 险种
     * @param RenewalInquiryPrinter $inquiryPrinter
     * @param $data
     * @return void
     * @version 4.0
     */
    private function setCoverage(RenewalInquiryPrinter $inquiryPrinter, $data)
    {
        foreach ($data as $coverage) {
            //交强险
            if ($coverage['KindCode'] == 'BZ') {
                $inquiryPrinter->ciEndData = ($coverage['EndDate'] ?? '') ? date('Y-m-d H:i:s', strtotime($coverage['EndDate'])) : '';

                if ($inquiryPrinter->ciEndData and date('H:i:s', strtotime($inquiryPrinter->ciEndData)) == '23:59:59') {
                    $inquiryPrinter->ciEndData = date('Y-m-d H:i:s', strtotime('+1 second', strtotime($inquiryPrinter->ciEndData)));
                }
                //商业险
            } else {
                $coveragePrinter = new PremiumCoveragePrinter();
                $coveragePrinter->coverageCode = array_search($coverage['KindCode'], ZFICEnums::$coverageCode);

                if (!$coveragePrinter->coverageCode) {
                    continue;
                }

                $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
                $coveragePrinter->amount = (string)(int)($coverage['SumInsured'] ?? '');
                $coveragePrinter->unitAmount = (string)(int)($coverage['UnitInsured'] ?? '');
                $coveragePrinter->quantity = $coverage['Quantity'] ?? '';

                $inquiryPrinter->setCoveragePrinterList($coveragePrinter);
            }
        }
    }
}
